package com.xucy.springboot.activemq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.ObjectMessage;
import java.util.List;

/**
 * @Author xucy
 * @Date 2019-07-05 10:38
 * @Description
 **/

@Component
public class MyConsumer {

    Logger logger= LoggerFactory.getLogger(MyConsumer.class);


    @JmsListener(destination = "myQueue",containerFactory = "jmsListenerContainerQueue")
    public void receiveStrQueue(String msg){
        logger.info(msg);
    }


    @JmsListener(destination = "myQueueList",containerFactory = "jmsListenerContainerQueue")
    public void receiveListQueue(List<String> list){
        logger.info("list的长度："+list.size());

        for (String s:list){
            logger.info(s);
        }
    }

    @JmsListener(destination="myQueueObject",containerFactory="jmsListenerContainerQueue")
    public void receiveObject(ObjectMessage objectMessage) throws Exception{
        User user=(User)objectMessage.getObject();
        logger.info(user.getName()+":"+user.getAge());
    }

    @JmsListener(destination="myQueueObjectList",containerFactory="jmsListenerContainerFactory")
    public void receiveObjectList(ObjectMessage objectMessage) throws Exception{
        List<Object> objects=(List<Object>)objectMessage.getObject();
        for(int i=0;i<objects.size();i++){
            User user=(User)objects.get(i);

            logger.info(user.getName()+":"+user.getAge());
        }
    }
}
